package com.hualan.sfzyproject.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzyproject.domain.PsyQuestion;
import com.hualan.sfzyproject.mapper.PsyQuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Service
public class PsyQuestionService {
    @Autowired
    private PsyQuestionMapper psyQuestionMapper;

    public List<PsyQuestion> getAllQuestion(int order){
        List<PsyQuestion> psyQuestion;
        if (order !=2){
            if (order == 0){
                PageHelper.orderBy("p.publish_time desc");
            }else if (order == 1){
                PageHelper.orderBy("r.reply_time desc");
            }
            List<PsyQuestion> psyQuestions = psyQuestionMapper.selectAll();
            psyQuestion = PageInfo.of(psyQuestions).getList();
        }else{
            List<PsyQuestion> psyQuestions = psyQuestionMapper.selectAll();
            for (int i = 0; i < psyQuestions.size()-1; i++) {
                for (int j = 0; j < psyQuestions.size()-1-i; j++) {
                    if(psyQuestions.get(j).getPsyTrueReplies().size() < psyQuestions.get(j+1).getPsyTrueReplies().size()){
                        PsyQuestion temp = psyQuestions.get(j);
                        psyQuestions.set(j,psyQuestions.get(j+1));
                        psyQuestions.set(j+1,temp);
                    }
                }
            }
            psyQuestion = psyQuestions;
        }
        for (PsyQuestion question : psyQuestion) {
            question.setReplyCount(question.getPsyTrueReplies().size());
        }
        return psyQuestion;
    }

    public List<PsyQuestion> getAllQuestion(int pageNum, int pageSize,int order) {
        List<PsyQuestion> psyQuestion;
        if (order !=2){
            if (order == 0){
                PageHelper.orderBy("p.publish_time desc");
            }else if (order == 1){
                PageHelper.orderBy("r.reply_time desc");
            }
            List<PsyQuestion> psyQuestions = psyQuestionMapper.selectAll();
            psyQuestion = PageInfo.of(psyQuestions).getList();
        }else{
            List<PsyQuestion> psyQuestions = psyQuestionMapper.selectAll();
            for (int i = 0; i < psyQuestions.size()-1; i++) {
                for (int j = 0; j < psyQuestions.size()-1-i; j++) {
                    if(psyQuestions.get(j).getPsyTrueReplies().size() < psyQuestions.get(j+1).getPsyTrueReplies().size()){
                        PsyQuestion temp = psyQuestions.get(j);
                        psyQuestions.set(j,psyQuestions.get(j+1));
                        psyQuestions.set(j+1,temp);
                    }
                }
            }
            psyQuestion = psyQuestions;
        }
        if (!psyQuestion.isEmpty()){
            psyQuestion = psyQuestion.subList((pageNum - 1) * pageSize, pageNum * pageSize);
        }
        for (PsyQuestion question : psyQuestion) {
            question.setReplyCount(question.getPsyTrueReplies().size());
        }
        return psyQuestion;
    }

    public PsyQuestion getByQuestionId(int questionId) {
        return psyQuestionMapper.selectByQuestionId(questionId);
    }


    public int insert(@RequestParam("title") String title,
                      @RequestParam("userId") Integer userId,
                      @RequestParam("content") String content) {
        return psyQuestionMapper.insert(title,userId,content);
    }


    public List<PsyQuestion> selectByLike(String like){
        List<PsyQuestion> psyQuestions = psyQuestionMapper.selectByLike(like);
        return psyQuestions;
    }

    public int delete(int questionId) {
        return psyQuestionMapper.deleteByQuestionId(questionId);
    }

}
